package com.ebo.upload;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.ebo.agora.openvcall.IBaseActivity;

import io.agora.openvcall.R;
import utils.FilePath;
import utils.Lg;

import static com.ebo.agora.openvcall.AGApplication.getInstance;

public class UploadActivity extends IBaseActivity implements View.OnClickListener {
    MultipartUpload multipartUpload;
    MultipartUpload multipartUpload2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);
        initViews();
        registerBroadCast();
    }

    UploadBroadcastReceiver uploadbroadcast;

    void registerBroadCast() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(getInstance().getResources().getString(R.string.broadcast_upload));
        uploadbroadcast = new UploadBroadcastReceiver();
        registerReceiver(uploadbroadcast, intentFilter);
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(uploadbroadcast);
        super.onDestroy();
    }


    Button btn_chooseFile, btn_pause;
    TextView tv_state;
    Button btn_chooseFile2, btn_pause2;
    TextView tv_state2;

    void initViews() {
        btn_chooseFile = (Button) findViewById(R.id.btn_chooseFile);
        btn_chooseFile.setOnClickListener(this);
        btn_chooseFile2 = (Button) findViewById(R.id.btn_chooseFile2);
        btn_chooseFile2.setOnClickListener(this);


        btn_pause = (Button) findViewById(R.id.btn_pause);
        btn_pause.setOnClickListener(this);
        btn_pause2 = (Button) findViewById(R.id.btn_pause2);
        btn_pause2.setOnClickListener(this);

        tv_state = (TextView) findViewById(R.id.state);
        tv_state2 = (TextView) findViewById(R.id.state2);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_pause:
                if(multipartUpload==null)
                {
                    return;
                }
                if (multipartUpload.isPause()) {
                    //继续
                    multipartUpload.resume();
                    btn_pause.setText("暂停");
                } else {
                    //暂停
                    multipartUpload.pause();
                    btn_pause.setText("继续");
                }
                break;
            case R.id.btn_pause2:
                if(multipartUpload2==null)
                {
                    return;
                }
                if (multipartUpload2.isPause()) {
                    //继续
                    multipartUpload2.resume();
                    btn_pause2.setText("暂停");
                } else {
                    //暂停
                    multipartUpload2.pause();
                    btn_pause2.setText("继续");
                }
                break;
            case R.id.btn_chooseFile2:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                        intent.setType("*/*");
                        startActivityForResult(intent, 101);
                    }
                }).start();
                break;
            case R.id.btn_chooseFile:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                        intent.setType("*/*");
                        startActivityForResult(intent, 100);
                    }
                }).start();
                break;

        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(data==null){
            Lg.d("data==null");
            return;
        }
        Uri Uri_UploadFile = data.getData();
        final String uploadFilePath = FilePath.getFileAbsolutePath(this, Uri_UploadFile);
        switch (requestCode) {
            case 100:
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        try {
//                            multipartUpload = new MultipartUpload(uploadFilePath);
//                            multipartUpload.multipartUpload();
//                        } catch (Exception e) {
//                            Log.e("upload error! " + e.toString());
//                        }
//                    }
//                }).start();
                break;
            case 101:
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        try {
//                            multipartUpload2 = new MultipartUpload(uploadFilePath);
//                            multipartUpload2.multipartUpload();
//                        } catch (Exception e) {
//                            Log.e("upload error! " + e.toString());
//                        }
//                    }
//                }).start();
                break;
        }
    }



    class UploadBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            if (intent.getAction().equals(getInstance().getResources().getString(R.string.broadcast_upload))) {
                Bundle bundle = intent.getExtras();
                int state = bundle.getInt(getInstance().getResources().getString(R.string.broadcast_upload_state));
                String object = bundle.getString("object");
                if (state == getInstance().getResources().getInteger(R.integer.broadcast_upload_success)) {
                    if(object.equals( multipartUpload.nameObject))
                    {
                        tv_state.setText("上传成功！");
//                        multipartUpload = null;
                    }else if(object.equals( multipartUpload2.nameObject)){
                        tv_state2.setText("上传成功！");
//                        multipartUpload2 = null;
                    }
                } else if (state == getInstance().getResources().getInteger(R.integer.broadcast_upload_faile)) {
                    if(object.equals( multipartUpload.nameObject))
                    {
                        tv_state.setText("上传失败！");
                    }else if(object.equals( multipartUpload2.nameObject)){
                        tv_state2.setText("上传失败！");
                    }
                } else if (state == getInstance().getResources().getInteger(R.integer.broadcast_upload_onProgress)) {
                    if(object.equals( multipartUpload.nameObject))
                    {
                        tv_state.setText("文件正在上传……" + bundle.getInt(getInstance().getResources().getString(R.string.broadcast_upload_progressNumber)) + "%");
                    }else if(object.equals( multipartUpload2.nameObject)){
                        tv_state2.setText("文件正在上传……" + bundle.getInt(getInstance().getResources().getString(R.string.broadcast_upload_progressNumber)) + "%");
                    }

                }
            }
        }
    }


}
